package com.loren.designer.web.interceptor;

import com.loren.designer.data.properties.DesignerProperties;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.mvc.ParameterizableViewController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
@AllArgsConstructor
public class FrontPageInterceptor implements HandlerInterceptor {

    private final DesignerProperties designerProperties;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (!(handler instanceof ParameterizableViewController)) {
            if (!StringUtils.endsWith(request.getRequestURI(), "/index.html")) {
                request.getRequestDispatcher(this.designerProperties.getFrontPage().getContext()).forward(request, response);
            }
        }
        return true;
    }

}
